package com.snail.app.gather.service;

import com.snail.app.gather.entity.DataSyncTriggerInfo;
import com.snail.app.gather.entity.DataSyncTriggerInfoId;
import com.snail.app.gather.rep.DataSyncTriggerInfoRep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @description
 * @date 2021/8/9
 * @autor houyanfeng
 */
@Service
@Transactional
public class DataSyncTriggerInfoService {
    @Autowired
    private DataSyncTriggerInfoRep dataSyncTriggerInfoRep;

    public DataSyncTriggerInfo getByKey(String id){
        return get(id,"update");
    }

    public DataSyncTriggerInfo getByKey(DataSyncTriggerInfoId id) {
        Optional optional = dataSyncTriggerInfoRep.findById(id);
        if (optional.isPresent()) {
            return (DataSyncTriggerInfo) optional.get();
        }
        return null;
    }
    public List<DataSyncTriggerInfo> listById(String id){
        return dataSyncTriggerInfoRep.execHql("from DataSyncTriggerInfo where id.id='"+id+"' and onoff=1 ");
    }
    public List<DataSyncTriggerInfo> listBySendPath(String sendPathId){
        return dataSyncTriggerInfoRep.execHql("from DataSyncTriggerInfo where sendPathId='"+sendPathId+"'");
    }

    public DataSyncTriggerInfo get(String id, String syncType){
        DataSyncTriggerInfoId dataSyncTriggerInfoId = new DataSyncTriggerInfoId(id, syncType);
        return getByKey(dataSyncTriggerInfoId);
    }

    public void save(DataSyncTriggerInfo entity) {
        dataSyncTriggerInfoRep.save(entity);
    }

}
